﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Drawing;

namespace displaycongress_desktop
{
    public class FileDecoder
    {
        public FileDecoder(String name, String str)
        {
            byte[] bytes = Decoder.decode(Decoder.HexString2Bytes(str));

            if (!Directory.Exists(Environment.GetEnvironmentVariable("LocalAppData") + "\\received_files\\"))
            {
                Directory.CreateDirectory(Environment.GetEnvironmentVariable("LocalAppData") + "\\received_files\\");
            }

            String[] extension = name.Split('.');
            String ext = extension[extension.Length - 1];

            if (ext.Equals("gif") || ext.Equals("png") || ext.Equals("jpg"))
            {
                saveAsImage(bytes, Environment.GetEnvironmentVariable("LocalAppData") + "\\received_files\\" + name);
            }
            else
            {
                File.WriteAllBytes(Environment.GetEnvironmentVariable("LocalAppData") + "\\received_files\\" + name, bytes);
            }
            Engine.sessionScreen.LoadFileViewer(Environment.GetEnvironmentVariable("LocalAppData") + "\\received_files\\" + name);
        }

        public void saveAsImage(byte[] byteArrayIn, String path)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image image = Image.FromStream(ms);
            image.Save(path);
        }
    }
}